package projectsupporter.common.element;

import java.util.LinkedHashMap;
import java.util.Map;

import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.graphics.Rectangle;

import projectsupporter.common.constant.Constant;

import dbTools.DBToolEngine;
import dbTools.tableInfo.info.TableInfo;

public class CodeModelCompsite extends Composite {

	private Map<String,TableInfo> tableInfos;
	
	private Table table = null;

	public CodeModelCompsite(Composite parent, int style) {
		super(parent, style);
		initialize();
		
		tableInfos= new LinkedHashMap<String,TableInfo>();
	}

	private void initialize() {
		table = new Table(this, SWT.MULTI | SWT.BORDER);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		table.setBounds(new Rectangle(8, 6, 104, 233));
		table.addMouseListener(new org.eclipse.swt.events.MouseAdapter() {
			public void mouseDown(org.eclipse.swt.events.MouseEvent e) {
				  TableItem[] items=table.getSelection();
				  if(items!=null && items.length>0 && e.button == 3)
				  {
					  showMenu();
				  }
			}
		});
		TableColumn col1 = new TableColumn(table, SWT.LEFT);
		col1.setText("TableName");  
		col1.setWidth(table.getSize().x);  
		TableColumn col2 = new TableColumn(table, SWT.LEFT);
		col2.setText("TableId");  
		col2.setWidth(0);
		col2.setResizable(false);
		
		this.setSize(new Point(581, 449));
		
		table.setSize(table.getSize().x, this.getSize().y);
		setLayout(null);
	}
	
	public void addSelectedItem(TreeItem[] items)
	{
		for(TreeItem item : items)
		{
			if(item.getData(Constant.ITEM_TYPE).equals(Constant.ITEM_TYPE_TABLE)==false)
			{
				continue;
			}
			
			TableInfo tableInfo=DBToolEngine.TABLE_INFO_MAP.get(item.getData(Constant.TABLE_KEY).toString());
			int itemIndex= getExistedItemIndex(tableInfo.getTableName());
			
			if(itemIndex==-1)
			{
				new TableItem(table, SWT.LEFT).setText(new String[] {tableInfo.getTableName(),tableInfo.getKey()});
				this.tableInfos.put(tableInfo.getKey(), tableInfo);
			}		
		}
		System.out.println("sus");
	}
	
	public void removeSelectedItem(TableItem[] items)
	{
		for(TableItem item : items)
		{
			TableInfo tableInfo=DBToolEngine.TABLE_INFO_MAP.get(item.getText(1));
			int itemIndex= getExistedItemIndex(tableInfo.getTableName());
			
			if(itemIndex >= 0)
			{
				table.remove(itemIndex);
				this.tableInfos.remove(tableInfo.getKey());
			}		
		}
		 
	}
	
	private int getExistedItemIndex(String tableName)
	{
		if(table.getItems()==null)
		{
			return -1;
		}
		
		for(int i= 0;i<table.getItemCount();i++)
		{
			TableItem item = table.getItem(i);
			if(item.getText(0).equals(tableName))
			{
				return i;
			}
		}
		
		return -1;
	}
	
	private void showMenu() {
		Menu tmpMenu = new Menu(this);         //增加右键菜单
        tmpMenu.setVisible(true);              //menu必须设置成visible才能显示出 
        setAddBaseCodeMenuItem(tmpMenu);
	}
	
	private void setAddBaseCodeMenuItem(Menu tmpMenu)
	{
		 MenuItem tmpItem = new MenuItem(tmpMenu, SWT.PUSH);
         tmpItem.setText("RemoveTable");          
         tmpItem.addSelectionListener(new SelectionListener() {
             
			@Override
			public void widgetDefaultSelected(SelectionEvent arg0) {
				System.out.println("DefaultSelected");
				
			}

			@Override
			public void widgetSelected(SelectionEvent arg0) {
				removeSelectedItem(table.getSelection());
				System.out.println("Selected");
			}
         });
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
